package com.blindBox.front.handler.registerHandler.registerHandlerImpl;

import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.dao_pojo.User;
import com.blindBox.front.exception.UserNotAutowiredException;
import com.blindBox.front.handler.mailHandler.MailHandler;
import com.blindBox.front.handler.registerHandler.AbstractRegisterHandler;
import com.blindBox.front.handler.tokenHandler.TokenHandler;
import com.blindBox.front.handler.verify.registerVerifyImpl.UserRegisterByAccountVerify;
import com.blindBox.front.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//账号和密码注册
@Component("accountRegisterHandler")
public class AccountRegisterHandler implements AbstractRegisterHandler
{
    private volatile User user;
    @Resource(name="userRegisterByAccountVerify")
    private UserRegisterByAccountVerify userRegisterByAccountVerify;
    @Autowired
    private UserService userService;
    @Autowired
    private TokenHandler tokenHandler;
    @Autowired
    private MailHandler mailHandler;
    //验证码
    private  volatile String code;
    public AccountRegisterHandler()
    {
        user=new User();
    }
    //注入属性
    public AccountRegisterHandler autowiredUser(User user)
    {
          this.user=user;
          return this;
    }
     public AccountRegisterHandler autowiredUser(String uname,String upwd,String virtualImgs,Integer sex,String mail,String code)
     {
         user=new User();
         user.setUname(uname);
         user.setUpwd(upwd);
         user.setVirtualImgs(virtualImgs);
         user.setSex(sex);
         user.setMail(mail);
         this.code=code;
         return this;
     }
     //保存用户数据到数据库
    public boolean saveUser(User user)
    {
        return userService.save(user);
    }
    //注册
    @Override
    public MsgBackToFront register() {
        try {
            //如果校验结果不是成功的，那么直接返回校验结果即可
            if(userRegisterByAccountVerify.verify(user).getStatus()!=MsgBackToFront.HANDLE_SUCCESS)
            {
                //用户名重复或邮箱重复注册账号
                 return userRegisterByAccountVerify.verify(user);
            }
            //验证码是否输入正确
            MsgBackToFront msgBackToFront = mailHandler.register_verify_eamil_login(user,user.getMail(), this.code);
            if(!msgBackToFront.getStatus().equals("handle_success"))
            return msgBackToFront;
            //用户名不重复
            //数据库操作出现异常
            if(!saveUser(user))
            {
                return new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
            }
            //注册成功,将用户名和密码加密成token返回
            String token = tokenHandler.encryptionUnameAndUpwd(user);
            return new MsgBackToFront(token,MsgBackToFront.HANDLE_SUCCESS);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //出现异常
        return new MsgBackToFront(null,MsgBackToFront.EXCEPTION_ERROR);
    }

    //注册的时候发送验证码
    public MsgBackToFront sendCode(String email) {
     return mailHandler.register_sendCode(email);
    }
}
